﻿using Masuit.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace UtilsBase
{
    public class UtilsDataTypeChange
    {
        #region 
        /// <summary>
        /// 十六进制字符串转十进制
        /// </summary>
        /// <param name="str">十六进制字符</param>
        /// <returns></returns>
        public int hex16Convert10(string str)
        {
            int res = 0;

            try
            {
                str = str.Trim().Replace(" ", "");//移除空字符
                //方法1
                res = int.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier);
                //方法2
                //int r2 = System.Int32.Parse(str, System.Globalization.NumberStyles.HexNumber);
                //Console.WriteLine(r2);
                //方法3
                //int r3 = Convert.ToInt32(str, 16);
                //Console.WriteLine(r3);

            }
            catch (Exception e)
            {
                res = 0;
            }

            return res;

        }
        public long MasuitHex16Convert10(string str)
        {
            //https://blog.csdn.net/sD7O95O/article/details/131606280
            //只支持小写转换
            str = str.ToLower();
            long res = 0;
            try
            {
                res = str.FromBase(16);
            }
            catch (Exception e)
            {
                res = 0;
            }
            return res;

            ////超大数字的进制转换
            //var num = "e6186159d38cd50e0463a55e596336bd".FromBaseBig(16); // 大数字16进制转10进制
            //Console.WriteLine(num); // 十进制：305849028665645097422198928560410015421
            //Console.WriteLine(num.ToBase(64)); // 64进制：3C665pQUPl3whzFlVpoPqZ，22位长度
            //Console.WriteLine(num.ToBase(36)); // 36进制：dmed4dkd5bhcg4qdktklun0zh，25位长度
            //Console.WriteLine(num.ToBase(7)); // 7进制：2600240311641665565300424545154525131265221035，46位长度
            //Console.WriteLine(num.ToBase(12)); // 12进制：5217744842749978a756b22135b16a5998a5，36位长度
            //Console.WriteLine(num.ToBase(41)); // 41进制：opzeBda2aytcEeudEquuesbk，24位长
        }



        #endregion
        /// <summary>
        /// 字符串转16位字节
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public byte[] strToHexByte(string hexString)

        {

            hexString = hexString.Replace(" ", "");

            if (hexString.Length % 2 != 0)

                hexString += " ";

            byte[] returnBytes = new byte[hexString.Length / 2];

            for (int i = 0; i < returnBytes.Length; i++)

                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

            return returnBytes;

        }
        #region 字节型转十六进制字符串

        /// <summary>

        /// 字节数组转16进制字符串

        /// </summary>

        /// <param name="bytes"></param>

        /// <returns></returns>

        public static string byteToHexStr(List<byte> bytes)

        {

            string returnStr = "";

            if (bytes != null)

            {

                for (int i = 0; i < bytes.Count; i++)

                {

                    returnStr += bytes[i].ToString("X2");

                }

            }

            return returnStr;

        }
        /// <summary>
        /// 16位字节转字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public string byteToHexStr(byte[] bytes)

        {

            string returnStr = "";

            if (bytes != null)
            {

                for (int i = 0; i < bytes.Length; i++)

                {
                    if (returnStr != "")
                    {
                        returnStr = returnStr + " ";
                    }
                    returnStr += bytes[i].ToString("X2");

                }

            }
            return returnStr;

        }
        #endregion

        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string byteToHexStr(byte[] bytes, int byteslength)
        {

            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < byteslength; i++)
                {
                    returnStr += bytes[i].ToString("X2") + " ";
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 二进制转换为十进制  把二进制数据"11011111"转换为十进制
        /// </summary>
        /// <param name="testStr"></param>
        /// <returns></returns>
        public static int binaryToDecimalStr(string testStr)
        {
            return Convert.ToInt32(testStr, 2);
        }
        /// <summary>
        /// 十进制转换为二进制 把十进制整数220转换为二进制：
        /// </summary>
        /// <param name="testValue"></param>
        /// <returns></returns>
        public static string decimaToBinaryTolStr(int testValue)
        {
            return Convert.ToString(testValue, 2);
        }
        /// <summary>
        /// 十六进制转二进制 
        /// </summary>
        /// <param name="hexString">FF9B</param>
        /// <returns></returns>
        public static string hexToBinaryTolStr(string hexString)
        {
            try
            {
                string[] s = hexString.Trim().Split(' '); //Trim先去掉前后空格，然后根据空格全部拆解，再组合。中间有多个空格也会去掉。
                StringBuilder sb = new StringBuilder();
                foreach (string item in s)
                {
                    if (item.Trim() != "")
                    {
                        sb.Append(item); //把为空的字符串组合起来
                    }
                }
                string result = string.Empty;
                foreach (char c in sb.ToString())
                {

                    int v = Convert.ToInt32(c.ToString(), 16);
                    int v2 = int.Parse(Convert.ToString(v, 2));
                    // 去掉格式串中的空格，即可去掉每个4位二进制数之间的空格，
                    result += string.Format("{0:d4} ", v2);
                }
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }

        }
        /// <summary>
        /// ieee754转换为十进制,gps定位结果解析用到
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static float IEEE754_Int32BitsToSingle(string hexStr)
        {
            hexStr = hexStr.Replace(" ", "").Replace("-", "");
            int intValue = Int32.Parse(hexStr, System.Globalization.NumberStyles.HexNumber);
            float floatValue = BitConverter.Int32BitsToSingle(intValue);
            return floatValue;
        }
      

    }
}
